<?php
namespace App\Admin\Controllers;
use App\Admin\Models\Activity;
use App\Admin\Models\HomeAd;
use App\Admin\Models\HomeAdCofig;
use App\Admin\Models\HomeCarousel;
use App\Admin\Models\HomeJdPdd;
use App\Admin\Models\HomeMenu;
use App\Admin\Models\HomeSearchBar;
use App\Admin\Models\HomeTemplatesConfig;
use App\Admin\Models\SourceMaterial;
use App\Admin\Models\ViewHomeAdCofig;
use App\Admin\Models\ViewHomeSelfCenter;
use Common\Common;
use Common\CommonAd;
use Common\ControllerAdBase;
use Phalcon\Mvc\Model\Transaction\Failed as TxFailed;
use Phalcon\Mvc\Model\Transaction\Manager as TxManager;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
use Services\Admin\StructureServices;

class StructureController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }


    /**
     * 获取所有可用的路由
     */
    public function homepathAction(){
        if(isset($this->params['keywords']) || empty($this->params['keywords'])){
            $this->params['keywords']="";
        }
        $StructureServices = new StructureServices();
        $getList = $StructureServices->homepath($this->params);
        $this->ok($getList);
    }



    /**
     * 添加修改路由素材
     */
    public function sourceeditAction()
    {
        if(!isset($this->params['name']) || empty($this->params['name'])){
            $this->fail(-1,'路由名称不能为空');
        }
        if(!isset($this->params['title']) || empty($this->params['title'])){
            $this->fail(-1,'路由副标不能为空');
        }
        if(!isset($this->params['path']) || empty($this->params['path'])){
            $this->fail(-1,'路由图标不能为空');
        }
        if(!isset($this->params['homepath_id']) || empty($this->params['homepath_id'])){
            $this->fail(-1,'路由地址不能为空');
        }
        if(!isset($this->params['platform']) || empty($this->params['platform'])){
            $this->fail(-1,'应用平台必选');
        }
        $StructureServices = new StructureServices();
        $responseResult = $StructureServices->sourceAddEdit($this->params);
        if($responseResult['status']){
            $this->ok([]);
        }
        $this->fail(-1,$responseResult['msg']);
    }

    /**
     * 路由详情
     */
    public function items_sourceAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '请传入路由id');
        }
        $StructureServices = new StructureServices();
        $responseResult = $StructureServices->itemsSource($this->params);
        if($responseResult['status']){
            $this->ok($responseResult['data']);
        }
        $this->fail(-1,$responseResult['msg']);

    }


    //总配置信息
    public function listAction()
    {
        $templates_config = new HomeTemplatesConfig();
        $list['items']             = $templates_config->find_list($this->params);
        $list['total_items'] = $templates_config->count();
        $this->ok($list);
    }

    /**
     * 修改颜色
     */
    public function config_editAction(){
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1,'系统错误');
        }
        $TemplateViewConfigDb = new HomeTemplatesConfig();
        $template_view_config = $TemplateViewConfigDb->findFirstById($this->params['id']);
        if(!$template_view_config){
            $this->fail(-1,'没有找到记录行');
        }
        $template_view_config->background_image_id = $this->params['background_image_id'];
        $template_view_config->carousel_background_image_id = $this->params['carousel_background_image_id'];
        $template_view_config->menu_background_image_id = $this->params['menu_background_image_id'];
        $template_view_config->font_color = $this->params['font_color'];
        $template_view_config->current_color = $this->params['current_color'];
        if($template_view_config->save()){
            $this->ok([]);
        }
        $this->fail(-1,'修改失败');
    }

    public function searchversionAction()
    {
        if (!isset($this->params['type']) || empty($this->params['type'])) {
            $this->fail(-1, '标识不存在');
        }
        if (!isset($this->params['version']) || empty($this->params['version'])) {
            $this->params['version'] = '';
        }
        switch ($this->params['type']) {
            case 'search_bar':
                $versionlist = new HomeTemplatesConfig();
                $result_sql  = $versionlist->versionlist($this->params['type'], $this->params);
                break;
            case 'carousel':
            case 'menu':
            case 'menu_top':
            case 'ad':
            case 'ad1':
            case 'ad2':
                $versionlist = new HomeTemplatesConfig();
                $result_sql  = $versionlist->versionlist($this->params['type']);
                break;
        }
        foreach ($result_sql as $key =>$value) {
            $result_sql[$key]['flag'] = $this->params['type'];
        }
        $this->ok($result_sql);
    }

    public function searchlistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 100;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $search    = new HomeSearchBar();
        $list      = $search->get_search_list();
        $paginator = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list      = $paginator->getPaginate();
        $this->ok($list);
    }


    public function routerlistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        $pageSize = 100;
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->params['ids'] = -1;
        }
        $sourcematerial = new SourceMaterial();
        $list           = $sourcematerial->get_route_list($this->params['ids']);
        $paginator      = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list           = $paginator->getPaginate();
        $list           = CommonAd::transition_array($list);
        $activityDb     = new Activity();
        foreach ($list['items'] as $key => $value) {
            if ($value['app_path'] == 'H5') {
                $list['items'][$key]['type'] = -2;
            }
            if (strpos($value['url'], 'activity-path/general/general?aid=') !== false) {
                $list['items'][$key]['type']    = -1;
                $aid_arr                        = explode('aid=', $value['url']);
                $aid                            = $aid_arr['1'];
                $activity_find_one              = $activityDb->find_by_id_obj($aid);
                $list['items'][$key]['ac_name'] = isset($activity_find_one->title) ? $activity_find_one->title : '';
            } else {
                $list['items'][$key]['ac_name'] = '';
            }
        }
        $this->ok($list);
    }


    /**
     * banner图列表
     */
    public function showcarouselAction()
    {
        $homecarousel = new HomeCarousel();
        if (!isset($this->params['carousel_id']) || empty($this->params['carousel_id'])) {
            $this->params['carousel_id'] = -1;
        }
        $list      = $homecarousel->find_list($this->params['carousel_id']);
        $paginator = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => 10,
                "page"  => 1
            )
        );
        $list      = $paginator->getPaginate();
        $this->ok($list);
    }

    /**
     * 首页菜单
     */
    public function showmenuAction()
    {
        $homemenu = new HomeMenu();
        if (!isset($this->params['menu_id']) || empty($this->params['menu_id'])) {
            $this->params['menu_id'] = -1;
        }
        $list      = $homemenu->find_list($this->params['menu_id']);
        $paginator = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => 10,
                "page"  => 1
            )
        );
        $list      = $paginator->getPaginate();
        $this->ok($list);
    }


    /**routerclear
     * 广告配置
     */
    public function showadsAction()
    {
        $adconfig = new HomeAdCofig();
        if (!isset($this->params['ad_id']) || empty($this->params['ad_id'])) {
            $this->params['ad_id'] = -1;
        }
        $list      = $adconfig->find_list($this->params['ad_id']);
        $paginator = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => 10,
                "page"  => 1
            )
        );
        $list      = $paginator->getPaginate();
        $this->ok($list);
    }


    /**
     * 查找路由图标
     */
    public function searchiconAction()
    {
        if (!isset($this->params['keywords']) && empty($this->params['keywords'])) {
            $this->params['keywords'] = '';
        }
        if (!isset($this->params['page']) && empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        if (!isset($this->params['limit']) && empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        $sourceMaterial        = new SourceMaterial();
        $path_list             = $sourceMaterial->get_path_list($this->params);
        $new['path']           = $path_list;
        $new['app_path']       = $path_list;
        $path_list_count       = $sourceMaterial->get_path_list($this->params, 'count');
        $new['path_total']     = $path_list_count;
        $new['app_path_total'] = $path_list_count;
        $sourceMaterial        = new SourceMaterial();
        //$path_list       = $sourceMaterial->get_App_path_list($this->params);
        //$path_list_count       = $sourceMaterial->get_App_path_list($this->params,'count');
        $this->ok($new);
    }

    /**
     * 选择路由
     */
    public function searchrouterAction()
    {
        if (!isset($this->params['router_ids']) || empty($this->params['router_ids'])) {
            $this->params['router_ids'] = -1;
        }
        if (!isset($this->params['keywords']) && empty($this->params['keywords'])) {
            $this->params['keywords'] = '';
        }
        if (!isset($this->params['page']) && empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        if (!isset($this->params['limit']) && empty($this->params['limit'])) {
            $this->params['limit'] = 5;
        }
        $in_router_arr  = explode(',', $this->params['router_ids']);
        $sourceMaterial = new SourceMaterial();
        $path_list      = $sourceMaterial->get_path_list($this->params);
        foreach ($path_list as $key => $value) {
            if (in_array($value['id'], $in_router_arr)) {
                $path_list[$key]['checked'] = true;
            } else {
                $path_list[$key]['checked'] = false;
            }
        }
        $count           = $sourceMaterial->get_path_list($this->params, 'count');
        $result['items'] = $path_list;
        $result['total'] = $count;
        $this->ok($result);
    }


    /**
     * 选择添加的路由
     */
    public function confirm_routerAction()
    {
        if (!isset($this->params['templates_config_id']) || empty($this->params['templates_config_id'])) {
            $this->fail(-1, '对不起，没有找到版本号');
        }
        if (!isset($this->params['router_cloumn_id']) || empty($this->params['router_cloumn_id'])) {
            $this->fail(-1, '对不起，没有找到模板');
        }
        if (!isset($this->params['cloumn_name']) || empty($this->params['cloumn_name'])) {
            $this->fail(-1, '对不起,没有找到模板名称');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '请选择路由');
        }
        $ids             = implode(',', $this->params['ids']);
        $templatesconfig = new HomeTemplatesConfig();
        $templates       = $templatesconfig->find_by_id($this->params['templates_config_id']);
        if (empty($templates)) {
            $this->fail(-1, '对不起，当前版本号不存在，或者已经被删除');
        }
        switch ($this->params['cloumn_name']) {
            case 'carousel':
                $carouselDb        = new HomeCarousel();
                $carousel_find_one = $carouselDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$carousel_find_one) {
                    $this->fail(-1, '没有找到轮播记录');
                }
                if ($carousel_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $carousel_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $carousel_find_one->ids = $ids;
                if (!$carousel_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            case 'search_bar':
                if (count($this->params['ids']) >= 3) {
                    $this->fail(-1, '搜索菜单只能设置两个');
                }
                $searchBarDb         = new HomeSearchBar();
                $search_bar_find_one = $searchBarDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$search_bar_find_one) {
                    $this->fail(-1, '没有找到搜索记录');
                }
                $search_bar_find_one->ids = $ids;
                if (!$search_bar_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            case 'menu':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->fail(-1, '没有找到菜单记录');
                }
                if ($menu_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $menu_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $menu_find_one->ids = $ids;
                if (!$menu_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            case 'ad':
            case 'ad1':
            case 'ad2':
                $ad1Db        = new HomeAd();
                $ad1_find_one = $ad1Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad1_find_one) {
                    $this->fail(-1, '没有找到广告记录');
                }
                if ($ad1_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $ad1_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $ad1_find_one->ids = $ids;
                if (!$ad1_find_one->save()) {
                    $this->fail(-1, '修改失败');
                }
                break;
            default:
                $this->fail(-1, '模块错误');
                break;
        }
        $this->ok([]);

    }


    /**
     * 添加路由
     */
    public function routeraddAction()
    {
        if (!isset($this->params['templates_config_id']) || empty($this->params['templates_config_id'])) {
            $this->fail(-1, '对不起，没有找到版本号');
        }
        if (!isset($this->params['router_cloumn_id']) || empty($this->params['router_cloumn_id'])) {
            $this->fail(-1, '对不起，没有找到模板');
        }
        if (!isset($this->params['cloumn_name']) || empty($this->params['cloumn_name'])) {
            $this->fail(-1, '对不起,没有找到模板名称');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '路由名称不能为空');
        }
        $this->dbMaster->begin();


        $templatesconfig = new ViewHomeTemplatesConfig();
        $templates       = $templatesconfig->find_by_id($this->params['templates_config_id']);
        if (empty($templates)) {
            $this->fail(-1, '对不起，当前版本号不存在，或者已经被删除');
        }

        switch ($this->params['type']) {
            case -1:
                $this->params['type'] = 2;
                break;
            case 2:
                $this->params['type']     = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case -2:
                $this->params['type']     = 2;
                $this->params['app_path'] = "H5";
                break;
        }

        $source_materialDb = new SourceMaterial();
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $source_materialDb->name          = $this->params['name'];
            $source_materialDb->path          = $this->params['path'];
            $source_materialDb->type          = $this->params['type'];
            $source_materialDb->app_path      = $this->params['app_path'];
            $source_materialDb->url           = $this->params['url'];
            $source_materialDb->app_data      = $this->params['app_data'];
            $source_materialDb->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_materialDb->is_need_login = $this->params['is_need_login'];
            $source_materialDb->title         = $this->params['title'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->end_color     = $this->params['end_color'];
            $source_materialDb->relationId    = $this->params['relationId'];
            if (!$source_materialDb->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '路由创建失败');
            }
        } else {
            $source_material_find_one = $source_materialDb->find_by_id_obj($this->params['id']);
            if (!$source_material_find_one) {
                $this->dbMaster->rollback();
                $this->fail(-1, '没有找到该路由记录，该路由已经被管理员删除');
            }
            $source_material_find_one->name          = $this->params['name'];
            $source_material_find_one->path          = $this->params['path'];
            $source_material_find_one->type          = $this->params['type'];
            $source_material_find_one->app_path      = $this->params['app_path'];
            $source_material_find_one->url           = $this->params['url'];
            $source_material_find_one->app_data      = $this->params['app_data'];
            $source_material_find_one->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_material_find_one->is_need_login = $this->params['is_need_login'];
            $source_material_find_one->title         = $this->params['title'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->end_color     = $this->params['end_color'];
            $source_material_find_one->relationId    = $this->params['relationId'];
            if (!$source_material_find_one->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '修改路由失败');
            }
        }
        switch ($this->params['cloumn_name']) {
            case 'carousel':
                $carouselDb        = new HomeCarousel();
                $carousel_find_one = $carouselDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$carousel_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到轮播记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($carousel_find_one->ids)) {
                        $carousel_find_one->ids = $source_materialDb->id;
                    } else {
                        $carousel_find_one->ids = $carousel_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$carousel_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改轮播记录失败');
                    }
                }
                break;
            case 'search_bar':
                $searchBarDb         = new HomeSearchBar();
                $search_bar_find_one = $searchBarDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$search_bar_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到搜索记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($search_bar_find_one->ids)) {
                        $search_bar_find_one->ids = $source_materialDb->id;
                    } else {
                        $search_bar_find_one->ids = $search_bar_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$search_bar_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改搜索记录失败');
                    }
                }
                break;
            case 'menu':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到菜单记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($menu_find_one->ids)) {
                        $menu_find_one->ids = $source_materialDb->id;
                    } else {
                        $menu_find_one->ids = $menu_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$menu_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改菜单记录失败');
                    }
                }
                break;
            case 'menu1':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到菜单记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($menu_find_one->ids)) {
                        $menu_find_one->ids = $source_materialDb->id;
                    } else {
                        $menu_find_one->ids = $menu_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$menu_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改菜单记录失败');
                    }
                }
                break;
            case 'ad1':
                $ad1Db        = new HomeAd();
                $ad1_find_one = $ad1Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad1_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到广告记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($ad1_find_one->ids)) {
                        $ad1_find_one->ids = $source_materialDb->id;
                    } else {
                        $ad1_find_one->ids = $ad1_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$ad1_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改广告记录失败');
                    }
                }
                break;
            case 'ad2':
                $ad2Db        = new HomeAd();
                $ad2_find_one = $ad2Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad2_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到广告2记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($ad2_find_one->ids)) {
                        $ad2_find_one->ids = $source_materialDb->id;
                    } else {
                        $ad2_find_one->ids = $ad2_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$ad2_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改广告2记录失败');
                    }
                }
                break;
            default:
                $this->fail(-1, '模块错误');
                break;
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }


    /**
     * 添加路由
     */
    public function routeradd_selfcenterAction()
    {
        if (!isset($this->params['templates_config_id']) || empty($this->params['templates_config_id'])) {
            $this->fail(-1, '对不起，没有找到版本号');
        }
        if (!isset($this->params['router_cloumn_id']) || empty($this->params['router_cloumn_id'])) {
            $this->fail(-1, '对不起，没有找到模板');
        }
        if (!isset($this->params['cloumn_name']) || empty($this->params['cloumn_name'])) {
            $this->fail(-1, '对不起,没有找到模板名称');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '路由名称不能为空');
        }
        $this->dbMaster->begin();


        $templatesconfig = new ViewHomeSelfCenter();
        $templates       = $templatesconfig->find_by_id($this->params['templates_config_id']);
        if (empty($templates)) {
            $this->fail(-1, '对不起，当前版本号不存在，或者已经被删除');
        }

        switch ($this->params['type']) {
            case -1:
                $this->params['type']     = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case 2:
                $this->params['type']     = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case -2:
                $this->params['type']     = 2;
                $this->params['app_path'] = "H5";
                break;
        }

        $source_materialDb = new SourceMaterial();
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $source_materialDb->name          = $this->params['name'];
            $source_materialDb->path          = $this->params['path'];
            $source_materialDb->type          = $this->params['type'];
            $source_materialDb->app_path      = $this->params['app_path'];
            $source_materialDb->url           = $this->params['url'];
            $source_materialDb->app_data      = $this->params['app_data'];
            $source_materialDb->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_materialDb->is_need_login = $this->params['is_need_login'];
            $source_materialDb->title         = $this->params['title'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->start_color   = $this->params['start_color'];
            $source_materialDb->end_color     = $this->params['end_color'];
            $source_materialDb->relationId    = $this->params['relationId'];
            if (!$source_materialDb->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '路由创建失败');
            }
        } else {
            $source_material_find_one = $source_materialDb->find_by_id_obj($this->params['id']);
            if (!$source_material_find_one) {
                $this->dbMaster->rollback();
                $this->fail(-1, '没有找到该路由记录，该路由已经被管理员删除');
            }
            $source_material_find_one->name          = $this->params['name'];
            $source_material_find_one->path          = $this->params['path'];
            $source_material_find_one->type          = $this->params['type'];
            $source_material_find_one->app_path      = $this->params['app_path'];
            $source_material_find_one->url           = $this->params['url'];
            $source_material_find_one->app_data      = $this->params['app_data'];
            $source_material_find_one->is_tb_atuh    = $this->params['is_tb_atuh'];
            $source_material_find_one->is_need_login = $this->params['is_need_login'];
            $source_material_find_one->title         = $this->params['title'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->start_color   = $this->params['start_color'];
            $source_material_find_one->end_color     = $this->params['end_color'];
            $source_material_find_one->relationId    = $this->params['relationId'];
            if (!$source_material_find_one->save()) {
                $this->dbMaster->rollback();
                $this->fail(-1, '修改路由失败');
            }
        }
        switch ($this->params['cloumn_name']) {
            case 'carousel':
                $carouselDb        = new HomeCarousel();
                $carousel_find_one = $carouselDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$carousel_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到轮播记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($carousel_find_one->ids)) {
                        $carousel_find_one->ids = $source_materialDb->id;
                    } else {
                        $carousel_find_one->ids = $carousel_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$carousel_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改轮播记录失败');
                    }
                }
                break;
            case 'search_bar':
                $searchBarDb         = new HomeSearchBar();
                $search_bar_find_one = $searchBarDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$search_bar_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到搜索记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($search_bar_find_one->ids)) {
                        $search_bar_find_one->ids = $source_materialDb->id;
                    } else {
                        $search_bar_find_one->ids = $search_bar_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$search_bar_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改搜索记录失败');
                    }
                }
                break;
            case 'men':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到菜单记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($menu_find_one->ids)) {
                        $menu_find_one->ids = $source_materialDb->id;
                    } else {
                        $menu_find_one->ids = $menu_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$menu_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改菜单记录失败');
                    }
                }
                break;
            case 'menu':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到菜单记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($menu_find_one->ids)) {
                        $menu_find_one->ids = $source_materialDb->id;
                    } else {
                        $menu_find_one->ids = $menu_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$menu_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改菜单记录失败');
                    }
                }
                break;
            case 'menu1':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到菜单记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($menu_find_one->ids)) {
                        $menu_find_one->ids = $source_materialDb->id;
                    } else {
                        $menu_find_one->ids = $menu_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$menu_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改菜单记录失败');
                    }
                }
                break;
            case 'ad1':
                $ad1Db        = new HomeAd();
                $ad1_find_one = $ad1Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad1_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到广告记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($ad1_find_one->ids)) {
                        $ad1_find_one->ids = $source_materialDb->id;
                    } else {
                        $ad1_find_one->ids = $ad1_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$ad1_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改广告记录失败');
                    }
                }
                break;
            case 'ad2':
                $ad2Db        = new HomeAd();
                $ad2_find_one = $ad2Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad2_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到广告2记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($ad2_find_one->ids)) {
                        $ad2_find_one->ids = $source_materialDb->id;
                    } else {
                        $ad2_find_one->ids = $ad2_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$ad2_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改广告2记录失败');
                    }
                }
                break;
            case 'ad3':
                $ad2Db        = new HomeAd();
                $ad2_find_one = $ad2Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad2_find_one) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '没有找到广告2记录');
                }
                if (isset($source_materialDb->id)) {
                    if (empty($ad2_find_one->ids)) {
                        $ad2_find_one->ids = $source_materialDb->id;
                    } else {
                        $ad2_find_one->ids = $ad2_find_one->ids . ',' . $source_materialDb->id;
                    }
                    if (!$ad2_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '修改广告2记录失败');
                    }
                }
                break;
            default:
                $this->fail(-1, '模块错误');
                break;
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }


    /**
     * 修改路由
     */
    public function routerupdateAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '素材id不能为空');
        }
        $sourcematerial = new SourceMaterial();
        $info           = $sourcematerial->find_by_id($this->params['id']);
        if (empty($info)) {
            $this->fail(-1, '该素材不存在，或已被删除');
        }
        $info['name']          = $this->params['name'];
        $info['path']          = $this->params['path'];
        $info['app_path']      = $this->params['app_path'];
        $info['type']          = $this->params['type'];
        $info['url']           = $this->params['url'];
        $info['app_data']      = $this->params['app_data'];
        $info['is_tb_atuh']    = $this->params['is_tb_atuh'];
        $info['is_need_login'] = $this->params['is_need_login'];
        $info['notice_id']     = $this->params['notice_id'];
        $info['start_color']   = $this->params['start_color'];
        $info['end_color']     = $this->params['end_color'];
        $info['relationId']    = $this->params['relationId'];
        $result                = $sourcematerial->save_params($info);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail('修改失败');
        }
    }


    /**
     * 移除路由
     */
    public function routerdelAction()
    {

    }

    /**
     * 清除路由
     */
    public function routerclearAction()
    {
        if (!isset($this->params['router_cloumn_id']) || empty($this->params['router_cloumn_id'])) {
            $this->fail(-1, '对不起，没有找到模板');
        }
        if (!isset($this->params['cloumn_name']) || empty($this->params['cloumn_name'])) {
            $this->fail(-1, '对不起,没有找到模板名称');
        }
        if (!isset($this->params['router_id']) || empty($this->params['router_id'])) {
            $this->fail(-1, '对不起,没有找到模板名称');
        }

        $sourceMaterialDb        = new SourceMaterial();
        $sourceMaterial_find_one = $sourceMaterialDb->find_by_id_obj($this->params['router_id']);
        if (!$sourceMaterial_find_one) {
            $this->fail(-1, '没有找到记录行');
        }
        switch ($this->params['cloumn_name']) {
            case 'carousel':
                $carouselDb        = new HomeCarousel();
                $carousel_find_one = $carouselDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$carousel_find_one) {
                    $this->fail(-1, '没有找到轮播记录');
                }
                $ids_arr = explode(',', $carousel_find_one->ids);
                if (!in_array($sourceMaterial_find_one->id, $ids_arr)) {
                    $this->fail(-1, '你选择的记录没有在该记录行');
                }
                $ids_arr                = array_diff($ids_arr, [$sourceMaterial_find_one->id]);
                $ids                    = implode(',', $ids_arr);
                $carousel_find_one->ids = $ids;
                if (!$carousel_find_one->save()) {
                    $this->fail(-1, '清除失败');
                }

                break;
            case 'search_bar':
                $searchBarDb         = new HomeSearchBar();
                $search_bar_find_one = $searchBarDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$search_bar_find_one) {
                    $this->fail(-1, '没有找到搜索记录');
                }
                $ids_arr = explode(',', $search_bar_find_one->ids);
                if (!in_array($sourceMaterial_find_one->id, $ids_arr)) {
                    $this->fail(-1, '你选择的记录没有在该记录行');
                }
                $ids_arr                  = array_diff($ids_arr, [$sourceMaterial_find_one->id]);
                $ids                      = implode(',', $ids_arr);
                $search_bar_find_one->ids = $ids;
                if (!$search_bar_find_one->save()) {
                    $this->fail(-1, '清除失败');
                }

                break;
            case 'menu':
            case 'men':
            case 'menu1':
            case 'menu2':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$menu_find_one) {
                    $this->fail(-1, '没有找到菜单记录');
                }
                $ids_arr = explode(',', $menu_find_one->ids);
                if (!in_array($sourceMaterial_find_one->id, $ids_arr)) {
                    $this->fail(-1, '你选择的记录没有在该记录行');
                }
                $ids_arr            = array_diff($ids_arr, [$sourceMaterial_find_one->id]);
                $ids                = implode(',', $ids_arr);
                $menu_find_one->ids = $ids;
                if (!$menu_find_one->save()) {
                    $this->fail(-1, '清除失败');
                }
                break;
            case 'ad':
            case 'ad1':
            case 'ad2':
            case 'ad3':
                $ad2Db        = new HomeAd();
                $ad2_find_one = $ad2Db->find_by_id_obj($this->params['router_cloumn_id']);
                if (!$ad2_find_one) {
                    $this->fail(-1, '没有找到广告2记录');
                }
                $ids_arr = explode(',', $ad2_find_one->ids);
                if (!in_array($sourceMaterial_find_one->id, $ids_arr)) {
                    $this->fail(-1, '你选择的记录没有在该记录行');
                }
                $ids_arr           = array_diff($ids_arr, [$sourceMaterial_find_one->id]);
                $ids               = implode(',', $ids_arr);
                $ad2_find_one->ids = $ids;
                if (!$ad2_find_one->save()) {
                    $this->fail(-1, '清除失败');
                }
                break;
            default:
                $this->fail(-1, '模块错误');
                break;
        }
        $this->ok([]);
    }


    /**
     * 添加版本配置信息 添加
     */
    public function templateconfigaddAction()
    {
        $this->templateconfigvalidate_field($this->params);
        if (!empty($this->params['background_image'])) {
            $source_material_params['name'] = $this->params['name'] . $this->params['version'] . '背景图';
            $source_material_params['path'] = $this->params['background_image'];
            $sourcematerial                 = new SourceMaterial();
            $sourcematerial->save_params($source_material_params);
            $info['background_image_id'] = $sourcematerial->id;
        }
        $info['name']                = $this->params['name'];
        $info['search_id']           = 1;//$this->params['search_id'] ? $this->params['search_id'] : 1;
        $info['carousel_id']         = $this->params['carousel_id'];
        $info['menu_id']             = $this->params['menu_id'];
        $info['menu_top']            = $this->params['menu_top'];
        $info['ad1_id']              = $this->params['ad_id'];
        $info['ad_id']               = $this->params['ad1_id'];
        $info['is_defalut']          = 1;
        $info['platform']            = $this->params['platform'] == 1 ? 'android' : 'ios';
        $info['is_common']           = 0;
        $info['version']             = $this->params['version'];
        $info['remark']              = $this->params['remark'];
        $info['func']                = 43;
        $info['background_image_id'] = 0;
        $templateconfig              = new HomeTemplatesConfig();
        $result                      = $templateconfig->save_params($info);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '新增配置失败');
        }
    }


    public function templateconfigdetailAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '配置id不能为空');
        }
        $hometemplateconfig = new HomeTemplatesConfig();
        $list_result        = $hometemplateconfig->find_by_id_union($this->params['id']);
        if (!$list_result) {
            $this->fail(-1, '该类模板配置不存在，或已被删除');
        }
        $this->ok($list_result);
    }


    public function templateconfigupdateAction()
    {
        $hometemplateconfig = new HomeTemplatesConfig();
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $hometemplateconfig->search_id   = $this->params['search_id'];
            $hometemplateconfig->carousel_id = $this->params['carousel_id'];
            $hometemplateconfig->menu_id     = $this->params['menu_id'];
            $hometemplateconfig->menu_top    = $this->params['menu_top'];
            $hometemplateconfig->ad1_id      = $this->params['ad1_id'];
            $hometemplateconfig->ad_id       = $this->params['ad_id'];
            $hometemplateconfig->name        = $this->params['name'];
            $hometemplateconfig->platform    = $this->params['platform'] == 1 ? 'android' : 'ios';
            $hometemplateconfig->version     = $this->params['version'];
            $hometemplateconfig->is_common   = $this->params['is_common'];
            $hometemplateconfig->is_defalut  = $this->params['is_defalut'];
            if (!$hometemplateconfig->save()) {
                $this->fail(-1, '添加失败');
            }
            $this->ok([]);
        } else {
            $info = $hometemplateconfig->find_by_id_obj($this->params['id']);
            if (!$info) {
                $this->fail(-1, '没有找到对应的配置');
            }
            $info->search_id   = $this->params['search_id'];
            $info->carousel_id = $this->params['carousel_id'];
            $info->menu_id     = $this->params['menu_id'];
            $info->ad_id       = $this->params['ad_id'];
            $info->ad1_id      = $this->params['ad1_id'];
            $info->ad2_id       = $this->params['ad2_id'];
            $info->name        = $this->params['name'];
            $info->platform    = $this->params['platform'] == 1 ? 'android' : 'ios';
            $info->version     = $this->params['version'];
            $info->is_common   = $this->params['is_common'];
            $info->is_defalut  = $this->params['is_defalut'];
            if (!$info->save()) {
                $this->fail(-1, '修改失败');
            }
            $this->ok([]);
        }
    }

    private function templateconfigvalidate_field($params)
    {
        if (!isset($params['platform']) || empty($params['platform'])) {
            $this->fail(-1, '平台不能为空');
        }
        if (!isset($params['version']) || empty($params['version'])) {
            $this->fail(-1, '版本不能为空');
        } else {
            $version_arr = explode('.', $params['version']);
            if (count($version_arr) != 3) {
                $this->fail(-1, '版本号不合法');
            }
            foreach ($version_arr as $key => $value) {
                if (!preg_match("/^\d*$/", $value)) {
                    echo $value;
                    $this->fail(-1, '版本号不合法');
                }
            }
        }
        $platform            = $params['platform'] == 1 ? 'android' : 'ios';
        $hometemplatesconfig = new HomeTemplatesConfig();
        $is_count_version    = $hometemplatesconfig->search_version_platform($params['version'], $platform);
        if ($is_count_version) {
            $this->fail(-1, '当前版本已经存在');
        }
        if (!isset($params['name']) || empty($params['name'])) {
            $this->fail(-1, '请为配置填写一个好区分的名称');
        }
    }

    /**
     * 菜单列表
     */
    public function menulistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 5;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $params['name'] = $this->params['name'];
        $db             = new HomeMenu();
        $list           = $db->get_list($params);
        $paginator      = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items     = $paginator->getPaginate();

        $newlist  = [];
        $sourceDb = new SourceMaterial();
        $data     = $this->transition_array($list_items->items);
        foreach ($data as $val) {
            $val['source_name'] = '';
            if (!empty($val['ids'])) {
                $name = $sourceDb->find([
                    "id in ({$val['ids']})",
                    'columns' => 'id,name',
                ])->toarray();
                if (!empty($name)) {
                    $source_name = array();
                    foreach ($name as $nval) {
                        $source_name[] = $nval['name'] . '.' . $nval['id'];
                    }
                    $val['source_name']     = implode(' , ', $source_name);
                    $val['source_name_arr'] = $source_name;
                } else {
                    $val['source_name']     = '';
                    $val['source_name_arr'] = [];
                }
            }
            $newlist[] = $val;
        }

        $list_items->items = $newlist;

        $this->ok($list_items);
    }

    public function transition_array($obj, $page = '')
    {
        $arr_str  = json_encode($obj);
        $arr_json = json_decode($arr_str, true);
        if (!empty($page)) {
            if ($arr_json['total_pages'] < $page) {
                $arr_json['items']   = [];
                $arr_json['current'] = $page;
                return $arr_json;
            }
        }
        return $arr_json;
    }

    /**
     * 编辑菜单
     */
    public function editmenuAction()
    {
        $id          = $this->params['id'];
        $tmp['name'] = $this->params['name'];
        $ids         = [];
        if (isset($this->params['ids']) && !empty($this->params['ids'])) {
            foreach ($this->params['ids'] as $val) {
                $temp = explode('.', $val);
                if (isset($temp[1])) {
                    $ids[] = $temp[1];
                }
            }
        }
        if (!empty($ids)) {
            $tmp['ids'] = implode(',', $ids);
        } else {
            $tmp['ids'] = '';
        }
        $db = new HomeMenu();
        if ($id > 0) {
            $save = $db->findFirstById($id)->update($tmp);
        } else {
            $save = $db->save($tmp);
        }
        if ($save) {
            $this->ok([]);
        } else {
            $this->fail(-1, $db->getMessage());
        }

    }

    /**
     * 广告列表
     */
    public function adcofiglistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 5;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $params['name'] = $this->params['name'];
        $db             = new ViewHomeAdCofig();
        $list           = $db->get_list($params);
        $paginator      = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items     = $paginator->getPaginate();

        $newlist = [];
        $dataDb  = new ViewHomeAd();
        $data    = $this->transition_array($list_items->items);
        foreach ($data as $val) {
            $val['source_name'] = '';
            if (!empty($val['ids'])) {
                $name = $dataDb->find([
                    "id in ({$val['ids']})",
                    'columns' => 'id,name',
                ])->toarray();
                if (!empty($name)) {
                    $source_name = array();
                    foreach ($name as $nval) {
                        $source_name[] = $nval['name'] . '.' . $nval['id'];
                    }
                    $val['source_name']     = implode(' , ', $source_name);
                    $val['source_name_arr'] = $source_name;
                } else {
                    $val['source_name']     = '';
                    $val['source_name_arr'] = [];
                }
            }
            $newlist[] = $val;
        }

        $list_items->items = $newlist;

        $this->ok($list_items);
    }

    /**
     * 子广告
     */
    public function getadlistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $db   = new ViewHomeAd();
        $list = $db->get_list([]);

        $valArr = [];
        if (!empty($list)) {
            $dataDb = new SourceMaterial();
            $data   = $this->transition_array($list);
            foreach ($data as &$val) {
                $temp['value']      = $val['name'] . '.' . $val['id'];
                $temp['label']      = $val['name'] . '.' . $val['id'];
                $valArr[]           = $temp;
                $val['source_name'] = '';
                if (!empty($val['ids'])) {
                    $name = $dataDb->find([
                        "id in ({$val['ids']})",
                        'columns' => 'id,name',
                    ])->toarray();
                    if (!empty($name)) {
                        $source_name = array();
                        foreach ($name as $nval) {
                            $source_name[] = $nval['name'] . '.' . $nval['id'];
                        }
                        $val['source_name']     = implode(' , ', $source_name);
                        $val['source_name_arr'] = $source_name;
                    } else {
                        $val['source_name']     = '';
                        $val['source_name_arr'] = [];
                    }
                }
            }
        }
        $result['data']  = $data;
        $result['value'] = $valArr;
        $this->ok($result);
    }

    /**
     * 编辑广告
     */
    public function adcofigeditAction()
    {

        $id          = $this->params['id'];
        $tmp['name'] = $this->params['name'];
        $ids         = [];
        if (isset($this->params['ids']) && !empty($this->params['ids'])) {
            foreach ($this->params['ids'] as $val) {
                $temp = explode('.', $val);
                if (isset($temp[1])) {
                    $ids[] = $temp[1];
                }
            }
        }
        if (!empty($ids)) {
            $tmp['ids'] = implode(',', $ids);
        } else {
            $tmp['ids'] = '';
        }
        $db = new HomeAdCofig();
        if ($id > 0) {
            $save = $db->findFirstById($id)->update($tmp);
        } else {
            $save = $db->save($tmp);
        }
        if ($save) {
            $this->ok([]);
        } else {
            $this->fail(-1, $db->getMessage());
        }

    }

    /**
     * 编辑子广告
     */
    public function adeditAction()
    {
        $tmp['name'] = $this->params['name'];
        $ids         = [];
        if (isset($this->params['ids']) && !empty($this->params['ids'])) {
            foreach ($this->params['ids'] as $val) {
                $temp = explode('.', $val);
                if (isset($temp[1])) {
                    $ids[] = $temp[1];
                }
            }
        }
        if (!empty($ids)) {
            $tmp['ids'] = implode(',', $ids);
        } else {
            $tmp['ids'] = '';
        }
        $db   = new HomeAd();
        $save = $db->save($tmp);
        if ($save) {
            $this->ok([]);
        } else {
            $this->fail(-1, $db->getMessage());
        }

    }

    /**
     * 个人中心列表
     */
    public function selfcenterAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $params['name'] = $this->params['name'];
        $db             = new ViewHomeSelfCenter();
        $list           = $db->get_list($params);
        $paginator      = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items     = $paginator->getPaginate();
        $list_items     = $this->transition_array($list_items);
        $menuDb         = new HomeMenu();
        $adcofigDb      = new ViewHomeAdCofig();
        $sourceDb       = new SourceMaterial();
        foreach ($list_items['items'] as $key => $val) {
            $center_find_one                          = $sourceDb->find_by_id($val['center_id']);
            $list_items['items'][$key]['center_name'] = $center_find_one ? $center_find_one['name'] : '未配置';
            $ad1config_find_one                       = $adcofigDb->find_by_id_obj($val['ad1_id']);
            $list_items['items'][$key]['ad1_name']    = $ad1config_find_one ? $ad1config_find_one->name : '未配置';
            $ad2config_find_one                       = $adcofigDb->find_by_id_obj($val['ad2_id']);
            $list_items['items'][$key]['ad2_name']    = $ad2config_find_one ? $ad2config_find_one->name : '未配置';
            $ad3config_find_one                       = $adcofigDb->find_by_id_obj($val['ad3_id']);
            $list_items['items'][$key]['ad3_name']    = $ad3config_find_one ? $ad3config_find_one->name : '未配置';
            $menu_find_one                            = $menuDb->find_by_id_obj($val['menu_id']);
            $list_items['items'][$key]['menu_name']   = $menu_find_one ? $menu_find_one->name : '未配置';
            $menu1_find_one                           = $menuDb->find_by_id_obj($val['menu1_id']);
            $list_items['items'][$key]['menu1_name']  = $menu1_find_one ? $menu1_find_one->name : '未配置';
            $men_find_one                             = $menuDb->find_by_id_obj($val['menu']);
            $list_items['items'][$key]['men_name']    = $men_find_one ? $men_find_one->name : '未配置';
        }
        $this->ok($list_items);
    }


    /**
     * 根据当前记录查找记录
     */
    public function self_center_find_oneAction()
    {
        if (!isset($this->params['templateconfig_id']) || empty($this->params['templateconfig_id'])) {
            $this->fail(-1, '没有找到当前版本');
        }
        $db                   = new ViewHomeSelfCenter();
        $self_center_find_one = $db->find_by_id($this->params['templateconfig_id']);
        if (!$self_center_find_one) {
            $this->fail(-1, '没有找到当前版本');
        }
        $menuDb                                 = new HomeMenu();
        $adcofigDb                              = new ViewHomeAdCofig();
        $sourceDb                               = new SourceMaterial();
        $center_find_one                        = $sourceDb->find_by_id($self_center_find_one['center_id']);
        $self_center_find_one['center_name']    = $center_find_one ? $center_find_one['name'] : '';
        $ad1config_find_one                     = $adcofigDb->find_by_id_obj($self_center_find_one['ad1_id']);
        $self_center_find_one['ad1_name']       = $ad1config_find_one ? $ad1config_find_one->name : '';
        $ad2config_find_one                     = $adcofigDb->find_by_id_obj($self_center_find_one['ad2_id']);
        $self_center_find_one['ad2_name']       = $ad2config_find_one ? $ad2config_find_one->name : '';
        $ad3config_find_one                     = $adcofigDb->find_by_id_obj($self_center_find_one['ad3_id']);
        $self_center_find_one['ad3_name']       = $ad3config_find_one ? $ad3config_find_one->name : '';
        $menu_find_one                          = $menuDb->find_by_id_obj($self_center_find_one['menu']);
        $self_center_find_one['menu_name_name'] = $menu_find_one ? $menu_find_one->name : '';
        $menu_find_one                          = $menuDb->find_by_id_obj($self_center_find_one['menu_id']);
        $self_center_find_one['menu_name']      = $menu_find_one ? $menu_find_one->name : '';
        $menu1_find_one                         = $menuDb->find_by_id_obj($self_center_find_one['menu1_id']);
        $self_center_find_one['menu1_name']     = $menu1_find_one ? $menu1_find_one->name : '';
        $this->ok($self_center_find_one);
    }

    public function getadcofigAction()
    {
        $db     = new HomeAdCofig();
        $find   = $db->find()->toarray();
        $result = [];
        if (!empty($find)) {
            foreach ($find as $val) {
                $result[] = $val['name'] . '.' . $val['id'];
            }
        }
        return $result;
    }

    public function gethomemenuAction()
    {
        $db     = new HomeMenu();
        $find   = $db->find()->toarray();
        $result = [];
        if (!empty($find)) {
            foreach ($find as $val) {
                $result[] = $val['name'] . '.' . $val['id'];
            }
        }
        return $result;
    }

    public function editcenterAction()
    {
        var_dump($this->params);
    }

    public function activityAction()
    {
        $db                  = new Activity();
        $type                = $this->params['type'];
        $where['conditions'] = '';
        if (!empty($type)) {
            $where['conditions'] = "type = $type";
        }
        $where['order'] = 'id desc';
        $list           = $db->find($where)->toarray();
        $this->ok($list);
    }

    public function editactivityAction()
    {
        $id                         = $this->params['id'];
        $tmp['title']               = $this->params['title'];
        $tmp['type']                = $this->params['type'];
        $tmp['activity_id']         = $this->params['activity_id'];
        $tmp['dec']                 = $this->params['dec'];
        $tmp['poster']              = $this->params['poster'];
        $tmp['share_img']           = $this->params['share_img'];
        $tmp['is_del']              = $this->params['is_del'] == 'true' ? 1 : 0;
        $tmp['bnt_colour']          = $this->params['bnt_colour'];
        $tmp['bnt_colour_gradient'] = $this->params['bnt_colour_gradient'];
        $tmp['bot_colour']          = $this->params['bot_colour'];
        $tmp['text_color']          = isset($this->params['text_color']) ? $this->params['text_color'] : '#000000';
        $tmp['copy_btn_color']      = isset($this->params['copy_btn_color']) ? $this->params['copy_btn_color'] : '#ff0000';
        $tmp['copy_text_color']     = isset($this->params['copy_text_color']) ? $this->params['copy_text_color'] : '#ffffff';
        $db                         = new Activity();
        if ($id > 0) {
            $save = $db->findFirstById($id)->update($tmp);
        } else {
            $save = $db->save($tmp);
        }
        if ($save) {
            $this->ok();
        } else {
            $this->fail(-1, '操作失败!');
        }
    }

//    public function delActi


    /**
     * 轮播列表
     */
    public function calistAction()
    {
        $db   = new HomeCarousel();
        $list = $db->get_list();
        $this->ok($list);
    }


    /**
     * 菜单列表
     */
    public function carousellistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        //搜索关键字
        if (!isset($this->params['keywords']) || empty($this->params['keywords'])) {
            $this->params['name'] = '';
        }
        $params['name'] = $this->params['name'];
        $db             = new HomeCarousel();
        $list           = $db->get_list($params);
        $paginator      = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items     = $paginator->getPaginate();

        $newlist  = [];
        $sourceDb = new SourceMaterial();
        $data     = $this->transition_array($list_items->items);
        foreach ($data as $val) {
            $val['source_name'] = '';
            if (!empty($val['ids'])) {
                $name = $sourceDb->find([
                    "id in ({$val['ids']})",
                    'columns' => 'id,name',
                ])->toarray();
                if (!empty($name)) {
                    $source_name = array();
                    foreach ($name as $nval) {
                        $source_name[] = $nval['name'] . '.' . $nval['id'];
                    }
                    $val['source_name']     = implode(' , ', $source_name);
                    $val['source_name_arr'] = $source_name;
                } else {
                    $val['source_name']     = '';
                    $val['source_name_arr'] = [];
                }
            }
            $newlist[] = $val;
        }

        $list_items->items = $newlist;

        $this->ok($list_items);
    }


    //修改轮播图
    public function addcarouselAction()
    {
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '轮播名称不能为空');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '轮播素材ids不能为空');
        }
        $homecarousel       = new HomeCarousel();
        $homecarousel->name = $this->params['name'];
        $homecarousel->ids  = $this->params['ids'];
        if ($homecarousel->save) {
            $this->ok([]);
        }
        $this->fail(-1, '轮播添加失败');

    }


    //添加广告
    public function addconfigadAction()
    {
        if (!isset($this->params['ad_name']) || empty($this->params['ad_name'])) {
            $this->fail(-1, '名称不能为空');
        }
        $this->dbMaster->begin();
        $template_config          = new HomeTemplatesConfig();
        $template_config_find_one = $template_config->find_by_id_obj($this->params['templates_config_id']);
        if (!$template_config_find_one) {
            $this->fail(-1, '没有找到版本配置');
        }
        switch ($this->params['cloumn_name']) {
            case 'ad':
                if (!isset($this->params['ad_config_name']) || empty($this->params['ad_config_name'])) {
                    $this->fail(-1, '主名称不能为空');
                }
                if (!isset($this->params['height']) || empty($this->params['height'])) {
                    $this->fail(-1, '高度不能为空');
                }
                $adConfigDB   = new HomeAdCofig();
                $adDb         = new HomeAd();
                $adDb->name   = $this->params['ad_name'];
                $adDb->height = $this->params['height'];
                if (!$adDb->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '保存失败');
                }
                if (!$template_config_find_one->ad_id) {
                    $adConfigDB->name = $this->params['ad_config_name'];
                    $adConfigDB->ids  = $adDb->id;
                    if (!$adConfigDB->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '保存失败');
                    }
                } else {
                    $adConfig_find_one = $adConfigDB->find_by_id_obj($template_config_find_one->ad_id);
                    if (!$adConfig_find_one) {
                        $this->fail(-1, '没有找到记录行');
                    }
                    $adConfig_find_one->name = $this->params['ad_config_name'];
                    $adConfig_find_one->ids  = $adConfig_find_one->ids ? $adConfig_find_one->ids . ",{$adDb->id}" : $adDb->id;
                    $adConfig_find_one->save();
                    if (!$adConfig_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '保存失败');
                    }
                }
                $template_config_find_one->ad_id = isset($adConfig_find_one->id) ? $adConfig_find_one->id : $adConfigDB->id;
                if (!$template_config_find_one->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '保存失败');
                }
                break;
            case 'ad1':
                if (!isset($this->params['ad_config_name']) || empty($this->params['ad_config_name'])) {
                    $this->fail(-1, '主名称不能为空');
                }
                if (!isset($this->params['height']) || empty($this->params['height'])) {
                    $this->fail(-1, '高度不能为空');
                }
                $adConfigDB   = new HomeAdCofig();
                $adDb         = new HomeAd();
                $adDb->name   = $this->params['ad_name'];
                $adDb->height = $this->params['height'];
                if (!$adDb->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '保存失败');
                }
                if (!$template_config_find_one->ad1_id) {
                    $adConfigDB->name = $this->params['ad_config_name'];
                    $adConfigDB->ids  = $adDb->id;
                    if (!$adConfigDB->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '保存失败');
                    }
                } else {
                    $adConfig_find_one = $adConfigDB->find_by_id_obj($template_config_find_one->ad1_id);
                    if (!$adConfig_find_one) {
                        $this->fail(-1, '没有找到记录行');
                    }
                    $adConfig_find_one->name = $this->params['ad_config_name'];
                    $adConfig_find_one->ids  = $adConfig_find_one->ids ? $adConfig_find_one->ids . ",{$adDb->id}" : $adDb->id;
                    $adConfig_find_one->save();
                    if (!$adConfig_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '保存失败');
                    }
                }
                $template_config_find_one->ad1_id = isset($adConfig_find_one->id) ? $adConfig_find_one->id : $adConfigDB->id;
                if (!$template_config_find_one->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '保存失败');
                }
                break;
            case 'ad2':
                if (!isset($this->params['ad_config_name']) || empty($this->params['ad_config_name'])) {
                    $this->fail(-1, '主名称不能为空');
                }
                if (!isset($this->params['height']) || empty($this->params['height'])) {
                    $this->fail(-1, '高度不能为空');
                }
                $adConfigDB   = new HomeAdCofig();
                $adDb         = new HomeAd();
                $adDb->name   = $this->params['ad_name'];
                $adDb->height = $this->params['height'];
                if (!$adDb->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '保存失败');
                }
                if (!$template_config_find_one->ad2_id) {
                    $adConfigDB->name = $this->params['ad_config_name'];
                    $adConfigDB->ids  = $adDb->id;
                    if (!$adConfigDB->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '保存失败');
                    }
                    $template_config_find_one->ad2_id = $adConfigDB->id;
                    if (!$template_config_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '保存失败');
                    }
                } else {
                    $adConfig_find_one = $adConfigDB->find_by_id_obj($template_config_find_one->ad2_id);
                    if (!$adConfig_find_one) {
                        $this->fail(-1, '没有找到记录行');
                    }
                    $adConfig_find_one->name = $this->params['ad_config_name'];
                    $adConfig_find_one->ids  = $adConfig_find_one->ids ? $adConfig_find_one->ids . ",{$adDb->id}" : $adDb->id;
                    if (!$adConfig_find_one->save()) {
                        $this->dbMaster->rollback();
                        $this->fail(-1, '保存失败');
                    }
                }
                break;
            case 'menu':
                if ($template_config_find_one->menu_id) {
                    $this->fail(-1, '该版本菜单已经配置');
                }
                $menuDb       = new HomeMenu();
                $menuDb->name = $this->params['ad_name'];
                if (!$menuDb->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '保存失败');
                }
                $template_config_find_one->menu_id = $menuDb->id;
                if (!$template_config_find_one->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '菜单版本保存失败');
                }
                break;
            case 'carousel':
                if ($template_config_find_one->carousel_id) {
                    $this->fail(-1, '该版本轮播已经配置');
                }
                $carouselDb       = new HomeCarousel();
                $carouselDb->name = $this->params['ad_name'];
                if (!$carouselDb->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '保存失败');
                }
                $template_config_find_one->carousel_id = $carouselDb->id;
                if (!$template_config_find_one->save()) {
                    $this->dbMaster->rollback();
                    $this->fail(-1, '轮播版本保存失败');
                }
                break;
            default:
                $this->fail(-1, '类型错误');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }


    /**
     * 选择默认的版本
     */
    public function is_default_versionAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '没有找到记录行');
        }
        $templateconfigDb        = new HomeTemplatesConfig();
        $templateconfig_find_one = $templateconfigDb->find_by_id_obj($this->params['id']);
        if (!$templateconfig_find_one) {
            $this->fail(-1, '没有找到记录行');
        }
        if ($templateconfig_find_one->is_common == 1) {
            $this->fail(-1, '至少选择一个默认版本');
        }
        if (!$templateconfigDb->batch_update()) {
            $this->fail(-1, '修改失败');
        }
        $update_true = $templateconfigDb->batch_version_update($templateconfig_find_one->version);
        if (!$update_true) {
            $this->fail(-1, '修改失败');
        }
        $this->ok([]);
    }


    /**
     * 个人中心首页模型获取
     */
    public function self_center_show_modelAction()
    {
        if (!isset($this->params['columns_name']) || empty($this->params['columns_name'])) {
            $this->fail(-1, '模块没有找到');
        }
        if (!isset($this->params['templateconfig_id']) || empty($this->params['templateconfig_id'])) {
            $this->fail(-1, '模块没有找到');
        }
        $selfCenterDb      = new ViewHomeSelfCenter();
        $template_find_one = $selfCenterDb->find_by_id_obj($this->params['templateconfig_id']);
        if (!$template_find_one) {
            $this->fail(-1, '没有找到记录行');
        }
        switch ($this->params['columns_name']) {
            case 'center':
                $sourceMaterialDb = new SourceMaterial();
                $get_list         = $sourceMaterialDb->get_route_list($template_find_one->center_id);
                break;

            case 'ad1':
                $adConfigDb = new ViewHomeAdCofig();
                $ad_id      = $template_find_one->ad1_id ? $template_find_one->ad1_id : -1;
                $ad1_find   = $adConfigDb->find_by_id_obj($ad_id);
                if ($ad1_find) {
                    $adDb     = new HomeAd();
                    $newids   = $ad1_find->ids ? $ad1_find->ids : -1;
                    $get_list = $adDb->find_list($newids);
                } else {
                    $get_list = [];
                }
                break;

            case 'ad2':
                $adConfigDb = new ViewHomeAdCofig();
                $ad_id      = $template_find_one->ad2_id ? $template_find_one->ad2_id : -1;
                $ad2_find   = $adConfigDb->find_by_id_obj($ad_id);
                if ($ad2_find) {
                    $adDb     = new HomeAd();
                    $newids   = $ad2_find->ids ? $ad2_find->ids : -1;
                    $get_list = $adDb->find_list($newids);
                } else {
                    $get_list = [];
                }
                break;

            case 'ad3':
                $adConfigDb = new ViewHomeAdCofig();
                $ad_id      = $template_find_one->ad3_id ? $template_find_one->ad3_id : -1;
                $ad3_find   = $adConfigDb->find_by_id_obj($ad_id);
                if ($ad3_find) {
                    $adDb     = new HomeAd();
                    $newids   = $ad3_find->ids ? $ad3_find->ids : -1;
                    $get_list = $adDb->find_list($newids);
                } else {
                    $get_list = [];
                }
                break;

            case 'men':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->find_list($template_find_one->menu);
                break;

            case 'menu':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->find_list($template_find_one->menu_id);
                break;

            case 'menu1':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->find_list($template_find_one->menu1_id);
                break;
            default:
                $this->fail(-1, '模块错误');
        }
        $this->ok($get_list);
    }


    /**
     * 添加模块名称
     */
    public function add_model_nameAction()
    {
        if (!isset($this->params['templateconfig_id']) || empty($this->params['templateconfig_id'])) {
            $this->fail(-1, '没有传递版本');
        }
        if (!isset($this->params['columns_name']) || empty($this->params['columns_name'])) {
            $this->fail(-1, '模块没有找到');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '名称不能为空');
        }
        $templateconfigDb  = new ViewHomeSelfCenter();
        $template_find_one = $templateconfigDb->find_by_id_obj($this->params['templateconfig_id']);
        if (!$template_find_one) {
            $this->fail(-1, '没有找到版本');
        }
        switch ($this->params['columns_name']) {
            case 'center':
                if ($template_find_one->center_id) {
                    $this->fail(-1, '已经指定了路由');
                }

                break;

            case 'ad1':
                if (!isset($this->params['height']) || empty($this->params['height'])) {
                    $this->fail(-1, '请填写广告的高度');
                }
                if ($template_find_one->ad1_id) {
                    $adDb         = new HomeAd();
                    $adDb->name   = $this->params['name'];
                    $adDb->height = $this->params['height'];
                    if (!$adDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $adConfigDb        = new HomeAdCofig();
                    $adConfig_find_one = $adConfigDb->find_by_id_obj($template_find_one->ad1_id);
                    if ($adConfig_find_one) {
                        $adConfig_find_one->name = $this->params['zhu_name'];
                        $adConfig_find_one->ids  = $adConfig_find_one->ids ? $adConfig_find_one->ids . ',' . $adDb->id : $adDb->id;
                        if (!$adConfig_find_one->save()) {
                            $this->fail(-1, '广告配置错误');
                        }
                    } else {
                        $adConfigDb       = new HomeAdCofig();
                        $adConfigDb->name = $this->params['zhu_name'];
                        $adConfigDb->ids  = $adDb->id;
                        if (!$adConfigDb->save()) {
                            $this->fail(-1, '保存广告失败');
                        }
                        $template_find_one->ad1_id = $adConfigDb->id;
                        if (!$template_find_one->save()) {
                            $this->fail(-1, '广告配置错误');
                        }
                    }
                } else {
                    $adDb         = new HomeAd();
                    $adDb->name   = $this->params['name'];
                    $adDb->height = $this->params['height'];
                    if (!$adDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $adConfigDb       = new HomeAdCofig();
                    $adConfigDb->name = $this->params['zhu_name'];
                    $adConfigDb->ids  = $adDb->id;
                    if (!$adConfigDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $template_find_one->ad1_id = $adConfigDb->id;
                    if (!$template_find_one->save()) {
                        $this->fail(-1, '广告配置错误');
                    }
                }
                break;

            case 'ad2':
                if (!isset($this->params['height']) || empty($this->params['height'])) {
                    $this->fail(-1, '请填写广告的高度');
                }
                if ($template_find_one->ad2_id) {
                    $adDb         = new HomeAd();
                    $adDb->name   = $this->params['name'];
                    $adDb->height = $this->params['height'];
                    if (!$adDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $adConfigDb        = new HomeAdCofig();
                    $adConfig_find_one = $adConfigDb->find_by_id_obj($template_find_one->ad2_id);
                    if ($adConfig_find_one) {
                        $adConfig_find_one->name = $this->params['zhu_name'];
                        $adConfig_find_one->ids  = $adConfig_find_one->ids ? $adConfig_find_one->ids . ',' . $adDb->id : $adDb->id;
                        if (!$adConfig_find_one->save()) {
                            $this->fail(-1, '广告配置错误');
                        }
                    } else {
                        $adConfigDb       = new HomeAdCofig();
                        $adConfigDb->name = $this->params['zhu_name'];
                        $adConfigDb->ids  = $adDb->id;
                        if (!$adConfigDb->save()) {
                            $this->fail(-1, '保存广告失败');
                        }
                        $template_find_one->ad2_id = $adConfigDb->id;
                        if (!$template_find_one->save()) {
                            $this->fail(-1, '广告配置错误');
                        }
                    }
                } else {
                    $adDb         = new HomeAd();
                    $adDb->name   = $this->params['name'];
                    $adDb->height = $this->params['height'];
                    if (!$adDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $adConfigDb       = new HomeAdCofig();
                    $adConfigDb->name = $this->params['zhu_name'];
                    $adConfigDb->ids  = $adDb->id;
                    if (!$adConfigDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $template_find_one->ad2_id = $adConfigDb->id;
                    if (!$template_find_one->save()) {
                        $this->fail(-1, '广告配置错误');
                    }
                }
                break;

            case 'ad3':
                if (!isset($this->params['height']) || empty($this->params['height'])) {
                    $this->fail(-1, '请填写广告的高度');
                }
                if ($template_find_one->ad3_id) {
                    $adDb         = new HomeAd();
                    $adDb->name   = $this->params['name'];
                    $adDb->height = $this->params['height'];
                    if (!$adDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $adConfigDb        = new HomeAdCofig();
                    $adConfig_find_one = $adConfigDb->find_by_id_obj($template_find_one->ad3_id);
                    if ($adConfig_find_one) {
                        $adConfig_find_one->name = $this->params['zhu_name'];
                        $adConfig_find_one->ids  = $adConfig_find_one->ids ? $adConfig_find_one->ids . ',' . $adDb->id : $adDb->id;
                        if (!$adConfig_find_one->save()) {
                            $this->fail(-1, '广告配置错误');
                        }
                    } else {
                        $adConfigDb       = new HomeAdCofig();
                        $adConfigDb->name = $this->params['zhu_name'];
                        $adConfigDb->ids  = $adDb->id;
                        if (!$adConfigDb->save()) {
                            $this->fail(-1, '保存广告失败');
                        }
                        $template_find_one->ad3_id = $adConfigDb->id;
                        if (!$template_find_one->save()) {
                            $this->fail(-1, '广告配置错误');
                        }
                    }
                } else {
                    $adDb         = new HomeAd();
                    $adDb->name   = $this->params['name'];
                    $adDb->height = $this->params['height'];
                    if (!$adDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $adConfigDb       = new HomeAdCofig();
                    $adConfigDb->name = $this->params['zhu_name'];
                    $adConfigDb->ids  = $adDb->id;
                    if (!$adConfigDb->save()) {
                        $this->fail(-1, '保存广告失败');
                    }
                    $template_find_one->ad3_id = $adConfigDb->id;
                    if (!$template_find_one->save()) {
                        $this->fail(-1, '广告配置错误');
                    }
                }
                break;
            case 'men':
                if ($template_find_one->menu) {
                    $this->fail(-1, '已经指定了路由');
                }
                $menuDb       = new HomeMenu();
                $menuDb->name = $this->params['name'];
                if (!$menuDb->save()) {
                    $this->fail(-1, '创建菜单失败');
                }
                $template_find_one->menu = $menuDb->id;
                if (!$template_find_one->save()) {
                    $this->fail(-1, '指定版本菜单失败');
                }
                break;
            case 'menu':
                if ($template_find_one->menu_id) {
                    $this->fail(-1, '已经指定了路由');
                }
                $menuDb       = new HomeMenu();
                $menuDb->name = $this->params['name'];
                if (!$menuDb->save()) {
                    $this->fail(-1, '创建菜单失败');
                }
                $template_find_one->menu_id = $menuDb->id;
                if (!$template_find_one->save()) {
                    $this->fail(-1, '指定版本菜单失败');
                }
                break;

            case 'menu1':
                if ($template_find_one->menu1_id) {
                    $this->fail(-1, '已经指定了路由');
                }
                $menuDb       = new HomeMenu();
                $menuDb->name = $this->params['name'];
                if (!$menuDb->save()) {
                    $this->fail(-1, '创建菜单失败');
                }
                $template_find_one->menu1_id = $menuDb->id;
                if (!$template_find_one->save()) {
                    $this->fail(-1, '指定版本菜单失败');
                }
                break;
            default:
                $this->fail(-1, '模块错误');
        }
        $this->ok([]);
    }


    /**
     * 个人中心选择路由
     */
    public function choose_self_center_routerAction()
    {
        if (!isset($this->params['columns_name']) || empty($this->params['columns_name'])) {
            $this->fail(-1, '模块没有找到');
        }
        if (!isset($this->params['templateconfig_id']) || empty($this->params['templateconfig_id'])) {
            $this->fail(-1, '模块没有找到');
        }
        if (!isset($this->params['router_columns_id']) || empty($this->params['router_columns_id'])) {
            $this->fail(-1, '没有找到当前路由');
        }
        $selfCenterDb      = new ViewHomeSelfCenter();
        $template_find_one = $selfCenterDb->find_by_id_obj($this->params['templateconfig_id']);
        if (!$template_find_one) {
            $this->fail(-1, '没有找到记录行');
        }
        $sourceMaterialDb = new SourceMaterial();
        switch ($this->params['columns_name']) {
            case 'center':
                $router_list = $sourceMaterialDb->get_route_list($template_find_one->center_id);
                if (!$router_list) {
                    $router_list = [];
                }
                break;

            case 'ad1':
                $adDb     = new HomeAd();
                $get_list = $adDb->find_by_id_obj($this->params['router_columns_id']);
                break;

            case 'ad2':
                $adDb     = new HomeAd();
                $get_list = $adDb->find_by_id_obj($this->params['router_columns_id']);
                break;

            case 'ad3':
                $adDb     = new HomeAd();
                $get_list = $adDb->find_by_id_obj($this->params['router_columns_id']);
                break;

            case 'men':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->find_by_id_obj($this->params['router_columns_id']);
                break;

            case 'menu':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->find_by_id_obj($this->params['router_columns_id']);
                break;

            case 'menu1':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->find_by_id_obj($this->params['router_columns_id']);
                break;
            default:
                $this->fail(-1, '模块错误');
        }
        if ($get_list) {
            $router_list = $sourceMaterialDb->get_route_list($get_list->ids);
            $activityDb  = new Activity();
            $router_list = $router_list->toarray();
            foreach ($router_list as $key => $value) {
                if (strpos($value['url'], 'activity-path/general/general?aid=') !== false) {
                    $router_list[$key]['type']    = -1;
                    $aid_arr                      = explode('aid=', $value['url']);
                    $aid                          = $aid_arr['1'];
                    $activity_find_one            = $activityDb->find_by_id_obj($aid);
                    $router_list[$key]['ac_name'] = isset($activity_find_one->title) ? $activity_find_one->title : '';
                } else {
                    $router_list[$key]['ac_name'] = '';
                }
            }
        } else {
            $router_list = [];
        }
        $this->ok($router_list);
    }

    /**
     * 个人中心选择路由
     */
    public function self_center_confirm_routerAction()
    {
        if (!isset($this->params['columns_name']) || empty($this->params['columns_name'])) {
            $this->fail(-1, '模块没有找到');
        }
        if (!isset($this->params['templateconfig_id']) || empty($this->params['templateconfig_id'])) {
            $this->fail(-1, '模块没有找到');
        }
        if (!isset($this->params['router_columns_id']) || empty($this->params['router_columns_id'])) {
            $this->fail(-1, '没有找到当前路由');
        }
        if (!isset($this->params['ids']) || empty($this->params['ids'])) {
            $this->fail(-1, '您没有选择路由');
        }
        $selfCenterDb      = new ViewHomeSelfCenter();
        $template_find_one = $selfCenterDb->find_by_id_obj($this->params['templateconfig_id']);
        if (!$template_find_one) {
            $this->fail(-1, '没有找到当前版本');
        }
        $ids = implode(',', $this->params['ids']);
        switch ($this->params['columns_name']) {
            case 'center':

                break;

            case 'ad1':
                $adDb        = new HomeAd();
                $ad_find_one = $adDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$ad_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if ($ad_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $ad_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $ad_find_one->ids = $ids;
                if (!$ad_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;

            case 'ad2':
                $adDb        = new HomeAd();
                $ad_find_one = $adDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$ad_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if ($ad_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $ad_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $ad_find_one->ids = $ids;
                if (!$ad_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;

            case 'ad3':
                $adDb        = new HomeAd();
                $ad_find_one = $adDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$ad_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if ($ad_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $ad_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $ad_find_one->ids = $ids;
                if (!$ad_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;

            case 'men':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$menu_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if ($menu_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $menu_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $menu_find_one->ids = $ids;
                if (!$menu_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;

            case 'menu':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$menu_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if ($menu_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $menu_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $menu_find_one->ids = $ids;
                if (!$menu_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;

            case 'menu1':
                $menuDb        = new HomeMenu();
                $menu_find_one = $menuDb->find_by_id_obj($this->params['router_columns_id']);
                if (!$menu_find_one) {
                    $this->fail(-1, '没有找到记录');
                }
                if ($menu_find_one->ids) {
                    $ids_new = explode(',', $ids);
                    $ids_old = explode(',', $menu_find_one->ids);
                    $ids_arr = array_unique(array_merge($ids_new, $ids_old));
                    $ids     = implode(',', $ids_arr);
                }
                $menu_find_one->ids = $ids;
                if (!$menu_find_one->save()) {
                    $this->fail(-1, '保存失败');
                }
                break;
            default:
                $this->fail(-1, '模块错误');
        }
        $this->ok([]);
    }


    //个人中心是否通用版本设置
    public function self_center_is_defaultAction()
    {
        if (!isset($this->params['templateconfig_id']) || empty($this->params['templateconfig_id'])) {
            $this->fail(-1, '模块没有找到');
        }
        $self_center          = new ViewHomeSelfCenter();
        $self_center_find_one = $self_center->find_by_id_obj($this->params['templateconfig_id']);
        if (!$self_center_find_one) {
            $this->fail(-1, '没有找到该版本');
        }

        if ($self_center_find_one->is_common == 1) {
            $this->fail(-1, '必选保留一个默认的版本');
        }

        if (!$self_center->batch_update()) {
            $this->fail(-1, '修改失败');
        }

        $version_batch = $self_center->batch_update_version($self_center_find_one->version);
        if (!$version_batch) {
            $this->fail(-1, '修改默认版本失败');
        }
        $this->ok([]);
    }


    //获取里面所有配置的版本
    public function searchappchildAction()
    {
        if (!isset($this->params['columns_name']) || empty($this->params['columns_name'])) {
            $this->fail(-1, '模块没有找到');
        }
        switch ($this->params['columns_name']) {
            case 'center':

                break;

            case 'ad1':
                $adConfigDb = new HomeAdCofig();
                $get_list   = $adConfigDb->get_list();
                break;

            case 'ad2':
                $adConfigDb = new HomeAdCofig();
                $get_list   = $adConfigDb->get_list();
                break;

            case 'ad3':
                $adConfigDb = new HomeAdCofig();
                $get_list   = $adConfigDb->get_list();
                break;

            case 'men':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->get_list();
                break;

            case 'menu':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->get_list();
                break;

            case 'menu1':
                $menuDb   = new HomeMenu();
                $get_list = $menuDb->get_list();
                break;
            default:
                $this->fail(-1, '模块错误');
        }
        $this->ok($get_list);
    }


    public function self_center_add_updateAction()
    {
        $selfCenterDb             = new ViewHomeSelfCenter();
        $this->params['platform'] = $this->params['platform'] == 1 ? 'android' : 'ios';
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $selfCenterDb->center_id  = 62;
            $selfCenterDb->ad1_id     = $this->params['ad1_id'];
            $selfCenterDb->menu       = $this->params['menu'];
            $selfCenterDb->menu_id    = $this->params['menu_id'];
            $selfCenterDb->menu1_id   = $this->params['menu1_id'];
            $selfCenterDb->ad2_id     = $this->params['ad2_id'];
            $selfCenterDb->ad3_id     = $this->params['ad3_id'];
            $selfCenterDb->platform   = $this->params['platform'];
            $selfCenterDb->is_defalut = 1;
            $selfCenterDb->is_common  = 0;
            $selfCenterDb->version    = $this->params['version'];
            if ($selfCenterDb->save()) {
                $this->ok([]);
            }
        } else {
            $self_center_find_one = $selfCenterDb->find_by_id_obj($this->params['id']);
            if (!$self_center_find_one) {
                $this->fail(-1, '对不起，没有找到对应的版本');
            }
            $self_center_find_one->ad1_id     = $this->params['ad1_id'];
            $self_center_find_one->menu       = $this->params['menu'];
            $self_center_find_one->menu_id    = $this->params['menu_id'];
            $self_center_find_one->menu1_id   = $this->params['menu1_id'];
            $self_center_find_one->ad2_id     = $this->params['ad2_id'];
            $self_center_find_one->ad3_id     = $this->params['ad3_id'];
            $self_center_find_one->platform   = $this->params['platform'];
            $self_center_find_one->is_defalut = 1;
            $self_center_find_one->version    = $this->params['version'];
            if ($self_center_find_one->save()) {
                $this->ok([]);
            }
        }
        $this->fail(-1, '操作失败');
    }


    /**
     * 显示菜单页面
     */
    public function show_menu_pageAction()
    {
        if (!isset($this->params['columns']) || empty($this->params['columns'])) {
            $this->fail(-1, '模块名称不存在');
        }
        switch ($this->params['columns']) {
            case 'jd_pdd_college':
                $jdPddDb      = new HomeJdPdd();
                $data['type'] = 'college';
                $get_list     = $jdPddDb->find_where_list($data);
                $menuDb       = new HomeMenu();
                foreach ($get_list as $key => $value) {
                    $menu_find_one         = $menuDb->find_by_id($value['menu_id']);
                    $get_list[$key]['ids'] = $menu_find_one ? $menu_find_one['ids'] : '';
                }

                break;
            case 'home_taobao':
                $homeTaobaoDb = new ViewHomeTaobao();
                $get_list     = $homeTaobaoDb->get_list();
                break;
            default:
                $this->fail(-1, '模块名称不存在');
        }
        $this->ok($get_list);
    }

    public function taobao_college_confirm_routerAction()
    {
        //id 10 view_home_menu  view_home_taobao
        if (!isset($this->params['columns']) || empty($this->params['columns'])) {
            $this->fail(-1, '抢沙发啊');
        }
        if (!isset($this->params['columns_id']) || empty($this->params['columns_id'])) {
            $this->fail(-1, '老板老是哟我们加班');
        }

        if ($this->params['columns'] == 'home_taobao') {
            $db = new ViewHomeTaobao();

            if (!empty($this->params['background_img'])) {
                if (isset($this->params['router_id']) && !empty($this->params['router_id'])) {
                    $sourceDb = new SourceMaterial();
                    $sinfo    = $sourceDb->findFirstById($this->params['router_id']);
                    if ($sinfo) {
                        $sourceDb = $sinfo;
                    }
                    $sourceDb->path = $this->params['background_img'];
                    $sourceDb->save();
                } else {
                    $sourceDb       = new SourceMaterial();
                    $sourceDb->path = $this->params['background_img'];
                    $sourceDb->save();
                }
            }
        } else {
            $db = new HomeMenu();
        }
        $info = $db->findFirstById($this->params['columns_id']);

        if (isset($this->params['ids'])) {
            if ($info->ids) {
                $ids_old   = explode(',', $info->ids);
                $ids_arr   = array_unique(array_merge($this->params['ids'], $ids_old));
                $info->ids = implode(',', $ids_arr);
            } else {
                $info->ids = implode(',', $this->params['ids']);
            }
        }
        if (isset($this->params['name'])) {
            $info->name = $this->params['name'];
        }
        if ($this->params['columns'] == 'home_taobao' && isset($sourceDb->id)) {
            $info->background_id = $sourceDb->id;
        }
        if ($info->save()) {
            $this->ok();
        }
        $this->fail(-1, '失败');
    }

    public function del_taobao_college_routerAction()
    {
        if (!isset($this->params['columns_id']) || empty($this->params['columns_id'])) {
            $this->fail(-1, 'columns_id不能为空');
        }
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        if ($this->params['columns'] == 'home_taobao') {
            $db = new ViewHomeTaobao();
        } else {
            $db = new HomeMenu();
        }
        $info = $db->findFirstById($this->params['columns_id']);
        if ($info) {
            $olg = explode(',', $info->ids);
            foreach ($olg as $key => $val) {
                if ($val == $this->params['id']) {
                    unset($olg[$key]);
                }
            }
            $info->ids = implode(',', $olg);
            if ($info->save()) {
                $this->ok('操作成功');
            }
        }
        $this->fail(-1, '操作失败!');
    }

    public function addsourceAction()
    {
        if (!isset($this->params['columns_id']) || empty($this->params['columns_id'])) {
            $this->fail(-1, '标签columns_id不能为空');
        }

        $this->dbMaster->begin();
        switch ($this->params['type']) {
            case -1:
                $this->params['type'] = 2;
                break;
            case 2:
                $this->params['type']     = 2;
                $this->params['app_path'] = 'H5UserId';
                break;
            case -2:
                $this->params['type']     = 2;
                $this->params['app_path'] = "H5";
                break;
        }

        $source_material_find_one = new SourceMaterial();
        if (isset($this->params['id']) && !empty($this->params['id'])) {
            $source_material_find_one = $source_material_find_one->find_by_id_obj($this->params['id']);
            if (!$source_material_find_one) {
                $this->dbMaster->rollback();
                $this->fail(-1, '没有找到该路由记录，该路由已经被管理员删除');
            }
        }

        $source_material_find_one->name          = $this->params['name'];
        $source_material_find_one->path          = $this->params['path'];
        $source_material_find_one->type          = $this->params['type'];
        $source_material_find_one->app_path      = $this->params['app_path'];
        $source_material_find_one->url           = $this->params['url'];
        $source_material_find_one->app_data      = $this->params['app_data'];
        $source_material_find_one->is_tb_atuh    = $this->params['is_tb_atuh'];
        $source_material_find_one->is_need_login = $this->params['is_need_login'];
        $source_material_find_one->title         = $this->params['title'];
        $source_material_find_one->start_color   = $this->params['start_color'];
        $source_material_find_one->end_color     = $this->params['end_color'];
        $source_material_find_one->relationId    = $this->params['relationId'];
        if (!$source_material_find_one->save()) {
            $this->dbMaster->rollback();
            $this->fail(-1, '修改路由失败');
        }

        if ($this->params['columns'] == 'home_taobao') {
            $db = new ViewHomeTaobao();
        } else {
            $db = new HomeMenu();
        }
        $info = $db->findFirstById($this->params['columns_id']);

        if (!$info) {
            $this->fail(-1, '不存在');
        }

        $info->ids = $info->ids ? $info->ids . ',' . $source_material_find_one->id : $source_material_find_one->id;
        if (!$info->save()) {
            $this->rollback();
            $this->fail(-1, '操作失败!');
        }
        $this->dbMaster->commit();
        $this->ok([]);
    }


    //删除个人中心的版本
    public function template_show_add_deleteAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $self_center          = new ViewHomeSelfCenter();
        $self_center_find_one = $self_center->find_by_id_obj($this->params['id']);
        if (!$self_center_find_one) {
            $this->fail(-1, '没有找到版本');
        }
        if ($self_center_find_one->delete()) {
            $this->ok([]);
        }
        $this->fail(-1, '操作失败');
    }


    //清除通用广告路由
    public function jd_pdd_clearrouterAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $jd_pdd_Db       = new HomeJdPdd();
        $jd_pdd_find_one = $jd_pdd_Db->find_by_id_obj($this->params['id']);
        if (!$jd_pdd_find_one) {
            $this->fail(-1, '没有找到该记录');
        }
        $jd_pdd_find_one->ad_id = '';
        if ($jd_pdd_find_one->save()) {
            $this->ok([]);
        }
        $this->fail(-1, '清除失败');
    }


    //清除首页版本
    public function templateconfigbtndeleteAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $templateConfigDb  = new HomeTemplatesConfig();
        $template_find_one = $templateConfigDb->find_by_id_obj($this->params['id']);
        if (!$template_find_one) {
            $this->fail(-1, '改版本不存在');
        }
        if ($template_find_one->delete()) {
            $this->ok([]);
        }
        $this->fail(-1, '删除失败');
    }

    public function update_lunboAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $view_home_ad = new HomeAd();
        $info         = $view_home_ad->findFirstById($this->params['id']);
        if (isset($this->params['height'])) {
            $info->height = $this->params['height'];
        }
        if (isset($this->params['is_carousel'])) {
            $info->is_carousel = $this->params['is_carousel'];
        }
        if (isset($this->params['name'])) {
            $info->name = $this->params['name'];
        }
        if ($info->save()) {
            $this->ok();
        }
        $this->fail(-1, '失败');
    }

    public function clear_ad_configAction()
    {
        if (!isset($this->params['ad_config_id']) || empty($this->params['ad_config_id'])) {
            $this->fail(-1, 'ad_config_id不能为空');
        }
        if (!isset($this->params['ad_id']) || empty($this->params['ad_id'])) {
            $this->fail(-1, 'ad_id不能为空');
        }

        $view_home_ad_cofig = new HomeAdCofig();
        $info               = $view_home_ad_cofig->findFirstById($this->params['ad_config_id']);
        if (!$info) {
            $this->fail(-1, '不存在!');
        }
        $ids = explode(',', $info->ids);
        if (!empty($ids)) {
            foreach ($ids as $key => $val) {
                if ($val == $this->params['ad_id']) {
                    unset($ids[$key]);
                }
            }
            if (!empty($ids)) {
                $info->ids = implode(',', $ids);
            } else {
                $info->ids = '';
            }
            if ($info->save()) {
                $this->ok();
            }
        }
        $this->fail(-1, '失败!');
    }


    public function routerdeleteAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $sourceDb        = new SourceMaterial();
        $source_find_one = $sourceDb->find_by_id_obj($this->params['id']);
        if (!$source_find_one) {
            $this->fail(-1, '该记录不存在或已被删除');
        }
        if ($source_find_one->delete()) {
            $this->ok([]);
        } else {
            $this->fail(-1, '删除失败');
        }
    }

    public function setsortAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $sourceDb        = new SourceMaterial();
        $source_find_one = $sourceDb->find_by_id_obj($this->params['id']);
        if (!$source_find_one) {
            $this->fail(-1, '该记录不存在或已被删除');
        }
        $source_find_one->sort = $this->params['sort'];
        if ($source_find_one->save()) {
            $this->ok([]);
        }
        $this->fail(-1, '设置失败');
    }


    public function update_search_params_wordsAction()
    {
        if (!isset($this->params['templates_config_id']) || empty($this->params['templates_config_id'])) {
            $this->fail(-1, '没有版本');
        }
        if (!isset($this->params['search_bar_id']) || empty($this->params['search_bar_id'])) {
            $this->fail(-1, '没有搜索');
        }
        $searchBarDb     = new HomeSearchBar();
        $search_find_one = $searchBarDb->find_by_id_obj($this->params['search_bar_id']);
        if (!$search_find_one) {
            $this->fail(-1, '没有找到配置记录');
        }
        $search_find_one->search_words = $this->params['search_words'];
        if ($search_find_one->save()) {
            $this->ok([]);
        }
        $this->fail(-1, '修改失败');
    }

    /**
     * 添加轮播背景颜色
     */
    public function router_carousel_color_changeAction()
    {
        $home_carouselDb = new HomeCarousel();
        $home_carousel   = $home_carouselDb->find_by_id_obj($this->params['id']);
        if (!$home_carousel) {
            $this->fail(-1, '没有找到记录行');
        }
        $home_carousel->start_color = $this->params['start_color'];
        $home_carousel->end_color   = $this->params['end_color'];
        if ($home_carousel->save()) {
            $this->ok([]);
        }
        $this->fail(-1, '修改失败');
    }

    /**
     * 广告排序
     */
    public function setadsortAction(){
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id！');
        }
        if(!isset($this->params['sort']) || empty($this->params['sort'])) {
            $this->fail(-1, 'sort！');
        }
        $view_home_ad = new ViewHomeAd();
        $view_home_ad = $view_home_ad->findFirstById($this->params['id']);
        IF($view_home_ad){
            $view_home_ad->sort = $this->params['sort'];
            if($view_home_ad->save()){
                $this->ok();
            }
        }
        $this->fail(-1,'操作失败!');
    }



    public function mslistAction()
    {

        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $params['name'] = $this->params['name'];
        $goods          = new SourceMaterial();
        $list           = $goods->get_list($params);
        $paginator      = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items     = $paginator->getPaginate();
        $list_items     = Common::transition_array($list_items);
        foreach ($list_items['items'] as $key => $value) {
            if ($value['app_path'] == 'H5') {
                $list_items['items'][$key]['type'] = -2;
            }
        }
        $this->ok($list_items);
    }


    public function sourcenoticeAction()
    {
        $notices = new ViewHomeNotice();
        $list    = $notices->find()->toarray();
        $this->ok($list);
    }


}
